import { createRouter, createWebHistory } from 'vue-router';

import Header from '../components/Header.vue';
import Regist from '../components/Regist.vue';
import Login from '../components/Login.vue';
import ShowSchedule from '../components/ShowSchedule.vue';

// router中导入pinia的时候，需要在defineUser中加入pinia对象
import pinia from '../pinia.js';
import { defineUser } from '../store/userStore.js';

let sysUser = defineUser(pinia);


const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            redirect: "/showSchedule",
        },
        {
            path: '/showSchedule',
            component: ShowSchedule,
        },
        {
            path: '/showSchedule',
            component: ShowSchedule,
        },
        {
            path: '/login',
            component: Login,
        },
        {
            path: '/regist',
            component: Regist,
        },
    ]
});

// 加入路由全局前置守卫判断能否访问showSchedule页面
// 注意还有默认情况下如果要跳转showSchedule页面，可以用重定向来解决
router.beforeEach((to, from, next) => {
    if (to.path === '/showSchedule') {
        // 登录就放行，没登录就回到登录页
        if (sysUser.username === '') {
            next('/login');
        } else {
            next();
        }
    } else {
        next();
    }
});



export default router;